package com.example.studentportrait.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.studentportrait.constants.SystemConstants;
import com.example.studentportrait.domain.ResponseResult;
import com.example.studentportrait.domain.entity.*;
import com.example.studentportrait.domain.vo.ItemListVo;
import com.example.studentportrait.domain.vo.PageVo;
import com.example.studentportrait.mapper.*;
import com.example.studentportrait.service.EmailService;
import com.example.studentportrait.service.ItemService;
import com.example.studentportrait.service.RoleService;
import com.example.studentportrait.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 项目认证表服务实现类
 *
 * @author makejava
 * @since 2023-10-17 21:18:32
 */
@Service("itemService")
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {

    @Autowired
    TeacherStudentMapper teacherStudentMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    DurationMapper durationMapper;

    @Autowired
    ComprehensiveAbilityMapper comprehensiveAbilityMapper;

    @Autowired
    EmailService emailService;

    //分页查询所管理的学生的项目
    @Override
    public ResponseResult itemList(Integer pageNum, Integer pageSize, Integer status) {
        // 获取当前登录教师账号
        String teacherAccount = "100513gzu";

        // 查询教师所管理的学生账号
        LambdaQueryWrapper<TeacherStudent> tsWrapper = new LambdaQueryWrapper<>();
        tsWrapper.eq(TeacherStudent::getTeacherAccount, teacherAccount);
        List<String> studentAccounts = teacherStudentMapper.selectList(tsWrapper)
                .stream()
                .map(TeacherStudent::getStudentAccount)
                .collect(Collectors.toList());

        // 如果没有管理的学生，直接返回空分页
        if (studentAccounts.isEmpty()) {
            PageVo emptyPage = new PageVo(Collections.emptyList(), 0L);
            return ResponseResult.okResult(emptyPage);
        }

        // 查询项目表
        LambdaQueryWrapper<Item> lqw = new LambdaQueryWrapper<>();
        // 查询是否删除状态
        lqw.eq(Item::getIsDelete, SystemConstants.NOT_DELETE);
        // 根据项目状态查询
        lqw.eq(status != null, Item::getStatus, status);
        // 根据学生账号查询
        lqw.in(Item::getStudentAccount, studentAccounts);

        // 分页查询
        Page<Item> page = new Page<>(pageNum, pageSize);
        page(page, lqw);

        // 获取查询结果
        List<Item> itemList = page.getRecords();

        // 转换为 ItemListVo
        List<ItemListVo> itemListVos = BeanCopyUtils.copyBeanList(itemList, ItemListVo.class);

        // 获取所有学生账号对应的姓名
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.in(User::getAccount, studentAccounts);
        List<User> users = userMapper.selectList(userWrapper);

        // 将用户姓名回设到 itemListVos
        Map<String, String> studentNameMap = users.stream()
                .collect(Collectors.toMap(User::getAccount, User::getUserName));  // 用户名字段是 `user_name`

        for (ItemListVo itemListVo : itemListVos) {
            String studentAccount = itemListVo.getStudentAccount();
            String studentName = studentNameMap.get(studentAccount);
            if (studentName != null) {
                itemListVo.setName(studentName);  // 设置学生姓名
            }
        }

        // 返回分页结果
        PageVo pageVo = new PageVo(itemListVos, page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    //教师端审核项目
    @Override
    public ResponseResult<Item> reviewItemById(Item item) {
        UpdateWrapper<Item> updateWrapper=new UpdateWrapper<>();
//        仅设置想要更新的字段
        updateWrapper.eq("id",item.getId())
                .set(item.getStatus()>=0,"status",item.getStatus())
                .set("review_time",new Date());
        //TODO 根据审核状态的不同调用不同方法

//        传入更新条件进行更新
        update(updateWrapper);
        return ResponseResult.okResult();
    }

}

